package utils;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import client.Callback_deprecated;

public class Common {

	// todos os produtos
	// [\+\-]?([A-Z]+\d+)([\*/]([A-Z]+\d+))+
	// todas as somas (adicionar o +)
	// [\+\-][A-Z]+\d\d+(?![/\*])
	// com digitos
	// [\+\-]\d\d+(?![/\*])

	public static final String SUM = "\\+";
	public static final String SUBTRACTION = "\\-*";
	public static final String MULTIPLICATION = "\\*";
	public static final String DIVISION = "/";

	public static final String OPERATORS = "[" + SUM + SUBTRACTION + MULTIPLICATION + DIVISION + "]";
	public static final String PRECEDENT_OPERATORS = "[" + MULTIPLICATION + DIVISION + "]";
	public static final String SUBSEQUENT_OPERATORS = "[" + SUM + SUBTRACTION + "]";

	public static final String ONE_OR_MORE_DIGITS = "\\d+";

	public static final String MORE_THAN_2_PRECEDENT_OPERATORS = ONE_OR_MORE_DIGITS + "(" + PRECEDENT_OPERATORS + ONE_OR_MORE_DIGITS + ")+" + PRECEDENT_OPERATORS + ONE_OR_MORE_DIGITS;
	public static final String TWO_PRECEDENT_OPERATORS = ONE_OR_MORE_DIGITS + PRECEDENT_OPERATORS + ONE_OR_MORE_DIGITS;

	public static final String ALPHANUMERIC_COORDINATE = "[A-Z]+" + ONE_OR_MORE_DIGITS;
	
	public static String GetColumnIndex(int intIndex) {
		String[] letters = new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

		String ans = "";
		while (intIndex > -1) {
			ans = letters[intIndex % 26] + ans;
			intIndex = intIndex / 26 - 1;
		}
		return ans;
	}

	public static int GetColumnIndex(String abscissa) {
		for (int i = 0; i < 1024; i++) {
			if (GetColumnIndex(i).equals(abscissa)) {
				return i;
			}
		}
		return -1;
	}

	public static List<String> getMatches(String regex, String characterSequence) {

		Pattern patt = Pattern.compile(regex);
		Matcher mat = patt.matcher(characterSequence);

		List<String> matches = new ArrayList<String>();
		while (mat.find()) {
			matches.add(mat.group());
		}

		return matches;

	}

	public static int[] GetCoordinates(String alphaNumCoordinates) {
		// abscissa
		List<String> matchResults = getMatches("[A-Z]+", alphaNumCoordinates);
		// TODO validar resultado
		String alphaAbscissa = matchResults.get(0);

		int[] coordinates = new int[2];
		coordinates[1] = GetColumnIndex(alphaAbscissa); // abscissa

		matchResults = getMatches("\\d+", alphaNumCoordinates);
		// TODO validar resultado
		coordinates[0] = Integer.parseInt(matchResults.get(0)) - 1;

		return coordinates;
	}

	public static boolean isDigit(String s) {
		return s.matches("\\d+");
	}
}